﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace ZhuXingFamilys.ExportExcel
{
    /**
     * 工程量清单条件过滤
     */

    internal class FilterForQuantities
    {
        // 过滤条件A 
        public static ObservableCollection<excelData> filter(String condition1, String condition2, String condition3, ObservableCollection<excelData> oldList)
        {
            
            if (condition3.Equals(""))
            {
                return oldList;
            }
            //System.Windows.Forms.MessageBox.Show(condition1 + condition2 + condition3);
            ObservableCollection<excelData> filterList = new ObservableCollection<excelData>();
            if (condition2.Equals("大于"))
            {
                if (condition1.Equals("Name") || condition1.Equals("Features") || condition1.Equals("Quantitiesname") || condition1.Equals("Company"))
                {
                    MessageBox.Show($"'{condition1}'不适用当前计算规则，请重新选择", "警告");
                }
                ulong xmbh = 0;
                double gcsl = 0;
                try
                {
                    xmbh = ulong.Parse(condition3);
                    gcsl = double.Parse(condition3);
                }
                catch (Exception)
                {
                    MessageBox.Show($"'{condition3}'不是数值，请重新填写", "警告");
                }
                switch (condition1)
                {
                    case "序号":
                        foreach (excelData item in oldList)
                        {
                            if (item.Id > int.Parse(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Code":
                        foreach (excelData item in oldList)
                        {
                            if (ulong.Parse(item.Code) > xmbh)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Num":
                        foreach (excelData item in oldList)
                        {
                            if (item.Num > gcsl)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                }
            }
            if (condition2.Equals("小于"))
            {
                if (condition1.Equals("Name") || condition1.Equals("Features") || condition1.Equals("Quantitiesname") || condition1.Equals("Company"))
                {
                    MessageBox.Show($"'{condition1}'不适用当前计算规则，请重新选择", "警告");
                }
                ulong xmbh = 0;
                double gcsl = 0;
                try
                {
                    xmbh = ulong.Parse(condition3);
                    gcsl = double.Parse(condition3);
                }
                catch (Exception)
                {
                    MessageBox.Show($"'{condition3}'不是数值，请重新填写", "警告");
                }
                switch (condition1)
                {
                    case "序号":
                        foreach (excelData item in oldList)
                        {
                            if (item.Id < int.Parse(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Code":
                        foreach (excelData item in oldList)
                        {
                            if (ulong.Parse(item.Code) < xmbh)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Num":
                        foreach (excelData item in oldList)
                        {
                            if (item.Num < gcsl)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                }
            }

            if (condition2.Equals("等于"))
            {
                ulong xmbh = 0;
                double gcsl = 0;
                if (condition1.Equals("序号") || condition1.Equals("Code") || condition1.Equals("Num"))
                {
                    try
                    {
                        xmbh = ulong.Parse(condition3);
                        gcsl = double.Parse(condition3);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show($"'{condition3}'不是数值，请重新填写", "警告");
                    }
                }
                
                switch (condition1)
                {
                    case "序号":
                        foreach (excelData item in oldList)
                        {
                            if (item.Id == int.Parse(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Code":
                        foreach (excelData item in oldList)
                        {
                            if (ulong.Parse(item.Code) == xmbh)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Name":
                        foreach (excelData item in oldList)
                        {
                            if (item.Name.Equals(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Features":
                        foreach (excelData item in oldList)
                        {
                            if (item.Features.Equals(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Quantitiesname":
                        foreach (excelData item in oldList)
                        {
                            if (item.Quantitiesname.Equals(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Company":
                        foreach (excelData item in oldList)
                        {
                            if (item.Company.Equals(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Num":
                        foreach (excelData item in oldList)
                        {
                            if (item.Num == gcsl)
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                }

            }

            if (condition2.Equals("包含"))
            {
                if (condition1.Equals("序号") || condition1.Equals("Num") || condition1.Equals("Quantitiesname") || condition1.Equals("Company"))
                {
                    MessageBox.Show($"'{condition1}'不适用当前计算规则，请重新选择", "警告");
                }
                switch (condition1)
                {
                    case "Code":
                        foreach (excelData item in oldList)
                        {
                            if (item.Code.Contains(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Name":
                        foreach (excelData item in oldList)
                        {
                            if (item.Name.Contains(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Features":
                        foreach (excelData item in oldList)
                        {
                            if (item.Features.Contains(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Quantitiesname":
                        foreach (excelData item in oldList)
                        {
                            if (item.Quantitiesname.Contains(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                    case "Company":
                        foreach (excelData item in oldList)
                        {
                            if (item.Company.Contains(condition3))
                            {
                                filterList.Add(item);
                            }
                        }
                        return filterList;
                }
            }
            return null;
        }
    }
}
